package grape.crm.service.customerbelong.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import grape.base.rest.client.dept.DeptClient;
import grape.base.rest.client.user.UserClient;
import grape.base.service.dept.po.Dept;
import grape.base.service.user.po.User;
import grape.crm.service.customerbelong.po.CustomerBelong;
import grape.crm.service.customerbelong.mapper.CustomerBelongMapper;
import grape.crm.service.customerbelong.api.ICustomerBelongService;
import grape.common.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 客户归属表 服务实现类
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
@Service
public class CustomerBelongServiceImpl extends BaseServiceImpl<CustomerBelongMapper, CustomerBelong> implements ICustomerBelongService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private DeptClient deptClient;
    /**
     * 重写添加客户归属
     * @param entity
     * @return
     */
    @Override
    public CustomerBelong insert(CustomerBelong entity) {
        setAttrForUpdateOrInsert(entity);

        return super.insert(entity);
    }

    @Override
    public boolean updateById(CustomerBelong entity) {
        setAttrForUpdateOrInsert(entity);
        return super.updateById(entity);
    }
    
    public CustomerBelong setAttrForUpdateOrInsert(CustomerBelong entity) {
        assertParamNotEmpty(entity.getCustomerId(),"customerId不能为空");
        assertParamNotEmpty(entity.getUserId(),"userId不能为空");
        User userById = userClient.getUserById(entity.getUserId());

        entity.setCompId(userById.getCompId());
        entity.setDeptId(userById.getDeptId());
        Dept deptById = deptClient.getDeptById(userById.getDeptId());

        entity.setDeptParentId(deptById.getParentId());
        entity.setDeptParentId1(deptById.getParentId1());
        entity.setDeptParentId2(deptById.getParentId2());
        entity.setDeptParentId3(deptById.getParentId3());
        entity.setDeptParentId4(deptById.getParentId4());
        entity.setDeptParentId5(deptById.getParentId5());
        entity.setDeptParentId6(deptById.getParentId6());
        entity.setDeptParentId7(deptById.getParentId7());
        entity.setDeptParentId8(deptById.getParentId8());
        entity.setDeptParentId9(deptById.getParentId9());
        entity.setDeptParentId10(deptById.getParentId10());

        return entity;
    }
}
